<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Register_jpc extends CI_Controller {
	
	public function __construct() 
	{
		parent::__construct();
		
		$this->load->model('model_jpc');
		$this->load->model('model_auth');
		
		$this->model_auth->_must_not_logged_in('default','error_register');
		
	
	}
	
	
	public function index()
	{	
		$this->load->view('header', 
			array('title'=> 'Registrasi ITB Junior Programming Contest 2011'));
		$data=array('image'=>$this->sam->_create_captcha());
		$this->load->view('jpc/form_jpc',$data);
		$this->load->view('footer');
	}
			
	public function _valid () {
		$this->valid = $this->form_validation;

		$this->valid->set_rules('jpc_nama','Nama anda','required|min_length[4]|max_length[50]');
		$this->valid->set_rules('jpc_nohp','Nomor ponsel anda','required|is_natural');
		$this->valid->set_rules('jpc_email','Alamat email anda','required|valid_email');
		$this->valid->set_rules('jpc_nama_sek','Nama sekolah','required|min_length[3]|max_length[100]');
		$this->valid->set_rules('jpc_alamat_sek','Alamat sekolah','required|min_length[3]|max_length[200]');
		$this->valid->set_rules('jpc_pembimbing','Nama pembimbing','required|min_length[3]|max_length[50]');
		
		$this->valid->set_rules('kode_verifikasi','Kode verifikasi','required|callback_check_captcha');
		$this->valid->set_rules('jpc_agreement','Persetujuan terhadap peraturan dan regulasi','required');
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->valid->set_message('check_captcha','%s salah.');
		$this->valid->set_message('required','%s wajib diisi.');
		$this->valid->set_message('min_length','%s minimal  terdiri dari %s karakter.');
		$this->valid->set_message('max_length','%s maksimal terdiri dari %s karakter.');
		$this->valid->set_message('is_natural','%s hanya boleh terdiri dari angka saja.');
		$this->valid->set_message('valid_email','Email yang anda cantumkan tidak valid.');
		
		return ($this->valid->run()==FALSE)? FALSE :TRUE ;
		
	}

	public function check_captcha() {
		// First, delete old captchas
		$expiration = time()-300; 
		$this->db->query("DELETE FROM captcha WHERE captcha_time < ".$expiration); 

		// Then see if a captcha exists:
		$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word = ? AND ip_address = ? AND captcha_time > ?";
		$binds = array($this->input->post('kode_verifikasi'), $this->input->ip_address(), $expiration);
		$query = $this->db->query($sql, $binds);
		$row = $query->row();

		return ($row->count==0)? FALSE :TRUE ;
	}

	
	public function validasi () {
	  // ["jpc_nama"]=>
	  // ["jpc_nohp"]=>
	  // ["jpc_email"]=>
	  // ["jpc_kelas"]=>
	  // ["jpc_nama_sek"]=>
	  // ["jpc_alamat_sek"]=>=
	  // ["jpc_pembimbing"]=>=
	  // ["kode_verifikasi"]=>=
	  // string(6) "Daftar"

		//var_dump($this->input->post());
		// echo validation_errors();
		
		if ($this->input->post()==NULL) {
			redirect('register_jpc/','location');
			}
 
		if ($this->_valid()==FALSE) { 
			// kembali ke form, tampilkan pesan error, apapun yang dimasukan tadi
			$this->index();
	
		} else {
		
			$data= $this->input->post();
			
			$this->load->view('header', 
				array('title'=> 'Finalisasi Registrasi ITB Senior Programming Contest 2011'));
			$this->load->view('jpc/finalisasi_jpc',$data);
			$this->load->view('footer');			
		
		}
	}
	
	public function finalisasi() {
		
		//print_r($this->input->post());
		if ($this->input->post()==NULL) {
			redirect('register_jpc/','location');
		}
 
		$res=$this->model_jpc->_create_peserta_junior($this->input->post());
		
		if ($res==0) show_error('Anda sudah terdaftar');
	
		$this->load->view('jpc/loading'); 		
	}
	
	public function sukses() {
		$this->load->view('header', 
				array('title'=> 'Finalisasi Registrasi ITB Senior Programming Contest 2011'));
		$this->load->view('jpc/sukses_jpc');
		$this->load->view('footer');	
	
	}
	
}
